import Mock from 'mockjs'


//用户
const obj = {
  id: '1',
  usertitle: '管理员',
  username: 'root',
  password: '123456',
  token: '13579'
}
Mock.mock('/login', 'post', (req) => {
  const uname = JSON.parse(req.body).username
  const pwd = JSON.parse(req.body).password
  if (uname === obj.username && pwd === obj.password) {
    return {
      status: true,
      token: obj.token
    }
  } else {
    return {
      status: false
    }
  }
})
Mock.mock('/userinfo', 'get', {
  userinfo: obj
})
//修改用户密码
Mock.mock('/userinfo/modify', 'post', (req) => {
  const pwdBefore = JSON.parse(req.body).password
  const pwdS = JSON.parse(req.body).passwordS
  const pwdSS = JSON.parse(req.body).passwordSS
  if (pwdBefore !== obj.password) {
    return {
      status: 0
    }
  } else if (pwdS !== pwdSS) {
    return {
      status: 1
    }
  } else {
    obj.password = pwdS
    return {
      status: 2
    }
  }
})

//药材
const medicine = [{
    id: 1,
    ycName: '红枣',
    ycAmount: 4000,
    ycPrice: 0.03
  },
  {
    id: 2,
    ycName: '山药',
    ycAmount: 1000,
    ycPrice: 0.02
  },
  {
    id: 3,
    ycName: '丁香',
    ycAmount: 2000,
    ycPrice: 0.06
  },
  {
    id: 4,
    ycName: '白芍',
    ycAmount: 1000,
    ycPrice: 0.04
  },
  {
    id: 5,
    ycName: '甘草',
    ycAmount: 2600,
    ycPrice: 0.02
  },
  {
    id: 6,
    ycName: '三七',
    ycAmount: 4000,
    ycPrice: 0.05
  },
  {
    id: 7,
    ycName: '杏仁',
    ycAmount: 1000,
    ycPrice: 0.03
  },
  {
    id: 8,
    ycName: '丹皮',
    ycAmount: 800,
    ycPrice: 0.04
  },
  {
    id: 9,
    ycName: '桂枝',
    ycAmount: 900,
    ycPrice: 0.03
  },
  {
    id: 10,
    ycName: '茯苓',
    ycAmount: 2000,
    ycPrice: 0.03
  }
]

//获取药材列表
Mock.mock('/information', 'post', (req) => {
  var info = JSON.parse(req.body)
  var [num, size, total, result] = [info.pageNum, info.pageSize, medicine.length, info.query]
  var medicineSearchList = []
  medicine.forEach((item, index) => {
    if (item.ycName.indexOf(result) >= 0) {
      medicineSearchList.push(item)
    }
  })
  //分页要展示的药材数据
  var medicinePageList = medicineSearchList.slice((num - 1) * size, num * size)
  return {
    data: {
      list: medicinePageList,
      total: total
    }
  }
})

//新增药材
Mock.mock('/information/add', 'post', (req) => {
  var addInfo = JSON.parse(req.body)
  var [yName, yMount, yPrice] = [addInfo.ycName, addInfo.ycAmount, addInfo.ycPrice]
  var flag = true
  var item
  medicine.forEach(item=>{
    if (yName === item.ycName) {
      flag = false
    }
  })
  if (flag === true) {
    var addObj = {
      id: medicine[medicine.length-1].id + 1,
      ycName: yName,
      ycAmount: parseFloat(yMount),
      ycPrice: parseFloat(yPrice)
    }
    medicine.push(addObj)
  }
  return {
    status: flag
  }
})

//根据id获取药材
Mock.mock(RegExp('/information/' + '.*'), 'get', (options) => {
  //var editInfo = JSON.parse(options.url)
  var editIdStr = options.url.slice(13)
  var editId = parseInt(editIdStr)
  var editMedicine = {}
  medicine.forEach((item, index) => {
    if (editId === medicine[index].id) {
      editMedicine = item
      return 'success'
    }
    return 'fail'
  })
  return {
    data: editMedicine,
    status: true
  }
})

//修改药材信息
Mock.mock('/information/modify', 'post', (req) => {
  const modifyInfo = JSON.parse(req.body)
  var [yId, yName, yMount, yPrice] = [modifyInfo.id, modifyInfo.ycName, modifyInfo.ycAmount, modifyInfo.ycPrice]
  var status = false
  medicine.forEach((item, index) => {
    if (yId === medicine[index].id) {
      item.ycName = yName
      item.ycAmount = yMount
      item.ycPrice = yPrice
      status = true
    }
  })
  return {
    status
  }
})

//删除药材信息
Mock.mock(RegExp('/del/' + '.*'), 'get', (options) => {
  //const delInfo = JSON.parse(req.body)
  var delStr = options.url.slice(5)
  var delId = parseInt(delStr)
  medicine.forEach((item, index) => {
    if (delId === medicine[index].id) {
      delete medicine[index]
    }
  })
  return {
    status: true
  }
})

//获取当前时间
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth()+1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
var dateStr = year + '-' +month + '-' + day + ' ' + hour + ':' + minute + ':' + second
var timestamp = new Date().getTime()

//入库单
const inDocument = [
  {
    id: 1594201730335,
    ycName: '红枣',
    inAmount: 500,
    ycPrice: 0.03,
    inPrice: 15,
    inDate: '2020-7-8 17:48:50'
  },
  {
    id: 1594201754680,
    ycName: '白芍',
    inAmount: 100,
    ycPrice: 0.04,
    inPrice: 4,
    inDate: '2020-7-8 17:49:14'
  },
  {
    id: 1594202012701,
    ycName: '白芍',
    inAmount: 200,
    ycPrice: 0.04,
    inPrice: 8,
    inDate: '2020-7-8 17:53:32'
  },
  {
    id: 1594202063027,
    ycName: '红枣',
    inAmount: 500,
    ycPrice: 0.03,
    inPrice: 15,
    inDate: '2020-7-8 17:54:23'
  },
  {
    id: 1594202837495,
    ycName: '红枣',
    inAmount: 500,
    ycPrice: 0.03,
    inPrice: 15,
    inDate: '2020-7-8 18:7:17'
  },
  {
    id: 1594202853678,
    ycName: '红枣',
    inAmount: 500,
    ycPrice: 0.03,
    inPrice: 15,
    inDate: '2020-7-8 18:7:33'
  }
]

//获取入库单数据
Mock.mock('/indata', 'post', (req) => {
  var inInfo = JSON.parse(req.body)
  var [num, size, total, result] = [inInfo.pageNum, inInfo.pageSize, inDocument.length, inInfo.query]
  var inSearchList = []
  inDocument.forEach((item, index) => {
    if (item.ycName.indexOf(result) >= 0) {
      inSearchList.push(item)
    }
  })
  //分页要展示的药材数据
  var inPageList = inSearchList.slice((num - 1) * size, num * size)
  return {
    data: {
      list: inPageList,
      total: total
    }
  }
})

//提交入库单
Mock.mock('/indata/add', 'post', (req) => {
  var inInfo = JSON.parse(req.body)
  var [yName, yMount] = [inInfo.ycName, inInfo.inAmount]
  var yPrice = 0
  var allPrice = 0
  var flag = false

  for(var i = 0;i < medicine.length; i++ ) {
    if(medicine[i] === undefined) {
      continue
    }else if(medicine[i].ycName === inInfo.ycName) {
      yPrice = medicine[i].ycPrice
      medicine[i].ycAmount += parseFloat(inInfo.inAmount)
      allPrice = medicine[i].ycPrice * inInfo.inAmount
      flag = true
      break
    } else if (i === medicine.length-1 && medicine[i].ycName !== inInfo.ycName) {
      flag = false
    }
  }

  if (flag === true) {
    var inObj = {
      id: timestamp,
      ycName: yName,
      inAmount: parseFloat(yMount),
      ycPrice: yPrice,
      inPrice: allPrice,
      inDate: dateStr
    }
    inDocument.push(inObj)
  }

  return {
    status: flag
  }
})

//删除药材信息
Mock.mock(RegExp('/indel/' + '.*'), 'get', (options) => {
  var delStr = options.url.slice(7)
  var delId = parseInt(delStr)
  inDocument.forEach((item, index) => {
    if (delId === inDocument[index].id) {
      delete inDocument[index]
    }
  })
  return {
    status: true
  }
})

//出库单
const outDocument = [
  {
    id: 1594304624366,
    ycName: '山药',
    outAmount: 200,
    ycPrice: 0.02,
    outPrice: 4,
    outDate: '2020-7-9 22:23:44',
    outDay: '7.9'
  },
  {
    id: 1594304650756,
    ycName: '丁香',
    outAmount: 100,
    ycPrice: 0.06,
    outPrice: 6,
    outDate: '2020-7-9 22:24:10',
    outDay: '7.9'
  },
  {
    id: 1594304667977,
    ycName: '甘草',
    outAmount: 200,
    ycPrice: 0.02,
    outPrice: 4,
    outDate: '2020-7-9 22:24:27',
    outDay: '7.9'
  },
  {
    id: 1594304773989,
    ycName: '山药',
    outAmount: 500,
    ycPrice: 0.02,
    outPrice: 10,
    outDate: '2020-7-9 22:26:13',
    outDay: '7.9'
  },
  {
    id: 1594304899178,
    ycName: '甘草',
    outAmount: 100,
    ycPrice: 0.02,
    outPrice: 2,
    outDate: '2020-7-9 22:28:19',
    outDay: '7.9'
  },
  {
    id: 1594443055603,
    ycName: '山药',
    outAmount: 200,
    ycPrice: 0.02,
    outPrice: 4,
    outDate: '2020-7-11 12:50:55',
    outDay: '7.11'
  },
  {
    id: timestamp,
    ycName: '山药',
    outAmount: 200,
    ycPrice: 0.02,
    outPrice: 4,
    outDate: dateStr,
    outDay: month+'.'+day
  }
]

//获取出库单数据
Mock.mock('/outdata', 'post', (req) => {
  var outInfo = JSON.parse(req.body)
  var [num, size, total, result] = [outInfo.pageNum, outInfo.pageSize, outDocument.length, outInfo.query]
  var outSearchList = []
  outDocument.forEach((item, index) => {
    if (item.ycName.indexOf(result) >= 0) {
      outSearchList.push(item)
    }
  })
  //分页要展示的药材数据
  var outPageList = outSearchList.slice((num - 1) * size, num * size)
  return {
    data: {
      list: outPageList,
      total: total
    }
  }
})

//提交出库单
Mock.mock('/outdata/add', 'post', (req) => {
  var outInfo = JSON.parse(req.body)
  var [yName, yMount] = [outInfo.ycName, outInfo.outAmount]
  var yPrice = 0
  var allPrice = 0
  var flag = ''

  for(var i = 0;i < medicine.length; i++ ) {
    if(medicine[i] === undefined) {
      continue
    } else if (yMount > medicine[i].ycAmount) {
      flag = 'error1'
      break
    } else if (medicine[i].ycName === outInfo.ycName) {
      yPrice = medicine[i].ycPrice
      medicine[i].ycAmount -= parseFloat(outInfo.outAmount)
      allPrice = medicine[i].ycPrice * outInfo.outAmount
      flag = 'success'
      break
    } else if (i === medicine.length-1 && medicine[i].ycName !== outInfo.ycName) {
      flag = 'error2'
    }
  }

  if (flag === 'success') {
    var outObj = {
      id: timestamp,
      ycName: yName,
      outAmount: parseFloat(yMount),
      ycPrice: yPrice,
      outPrice: allPrice,
      outDate: dateStr
    }
    outDocument.push(outObj)
  }

  return {
    status: flag
  }
})

//删除药材信息
Mock.mock(RegExp('/outdel/' + '.*'), 'get', (options) => {
  var delStr = options.url.slice(8)
  var delId = parseInt(delStr)
  outDocument.forEach((item, index) => {
    if (delId === outDocument[index].id) {
      delete outDocument[index]
    }
  })
  return {
    status: true
  }
})

//单种药材出库统计
Mock.mock(RegExp('/chart/' + '.*'), 'get', (options) => {
  var chartStr = options.url.slice(7)//拿到药材名称
  var singleList = outDocument.filter(item => {
    return (item.ycName === chartStr)
  })
  var flag = ''
  if(singleList.length === 0) {
    flag = 'error'
  }
  var dateSplit = singleList.map(item => {
    return item.outDay
  })
  var dateDay = []//去除了重复的日期
  for(var i = 0; i < dateSplit.length; i++) {
    if(dateDay.indexOf(dateSplit[i]) == -1) {
      dateDay.push(dateSplit[i])
    }
  }
  var dateArr = []
  dateDay.forEach(item => {
    dateArr.push({flagDate: item, amount: 0, price: 0})
  })
  singleList.forEach(item1=>{
    dateArr.forEach(item2=>{
      if(item1.outDay === item2.flagDate) {
        item2.amount += item1.outAmount
        item2.price += item1.outPrice
      }
    })
  })
  //日期数组
  var dateArrDate = dateArr.map(item => {
    return item.flagDate
  })
  //数量数组
  var dateArrAmount = dateArr.map(item => {
    return item.amount
  })
  var totalPrice = 0
  //总价数组
  dateArr.forEach(item => {
    totalPrice += item.price
  })
  var optionsObj = {
    title: {
      text: chartStr+'出库情况'
    },
    tooltip: {},
    legend: {},
    xAxis: {
      data: dateArrDate
    },
    yAxis: {},
    series: [{
      name: '克数',
      type: 'line',
      data: dateArrAmount
    }]
  }
  return {
    data: optionsObj,
    yc: chartStr,
    status: flag,
    total: totalPrice,
    amount: dateArrAmount
  }
})

//全部药材出库统计
Mock.mock(RegExp('/allchart/' + '.*'), 'get', (options) => {
  var allChartStr = options.url.slice(10)
  var flag = ''
  if(allChartStr.indexOf('-') === -1){
    flag = 'error1'
    return {
      status: flag
    }
  } else {
    var allChartStrArr = allChartStr.split("-")
    var [allChartNum1, allChartNum2] = [parseFloat(allChartStrArr[0].split(".").join("")), parseFloat(allChartStrArr[1].split(".").join(""))]
    var allChartList = outDocument.filter(item => {
      return (parseFloat(item.outDay.split(".").join("")) >= allChartNum1) && parseFloat(item.outDay.split(".").join("")) <= allChartNum2
    })
    if(allChartList.length === 0) {
      flag = 'error'
    }
    var ycNameSplit = allChartList.map(item => {
      return item.ycName
    })
    var ycNameSingle = []//去除了重复的名字
    for(var i = 0; i < ycNameSplit.length; i++) {
      if(ycNameSingle.indexOf(ycNameSplit[i]) == -1) {
        ycNameSingle.push(ycNameSplit[i])
      }
    }

    var ycNameArr = []
    ycNameSingle.forEach(item => {
      ycNameArr.push({flagYcName: item, amount: 0, price: 0})
    })
    allChartList.forEach(item1=>{
      ycNameArr.forEach(item2=>{
        if(item1.ycName === item2.flagYcName) {
          item2.amount += item1.outAmount
          item2.price += item1.outPrice
        }
      })
    })
    //药材名称数组
    var ycNameArrDate = ycNameArr.map(item => {
      return item.flagYcName
    })
    //数量数组
    var ycNameArrAmount = ycNameArr.map(item => {
      return item.amount
    })
    var totalPrice = 0
    //总价数组
    ycNameArr.forEach(item => {
      totalPrice += item.price
    })
    var optionsObj2 = {
      title: {
        text: '出库情况'
      },
      tooltip: {},
      legend: {},
      xAxis: {
        data: ycNameArrDate
      },
      yAxis: {},
      series: [{
        name: '克数',
        type: 'bar',
        data: ycNameArrAmount
      }]
    }
    return {
      data: optionsObj2,
      status: flag,
      total: totalPrice,
      amount: ycNameArrAmount
    }
  }

})

//获取药材数组长度

Mock.mock('/mlength','get', {
  length: medicine.length
})

//获取用户数组长度
Mock.mock('/ulength','get', {
  name: obj.usertitle,
  length: 1
})

//获取入库数组长度
Mock.mock('/ilength','get', {
  length: inDocument.length
})

//获取出库数组长度
Mock.mock('/olength','get', {
  length: outDocument.length
})
